<!DOCTYPE html>
<html>
<head>
    <title>Electromagnetic Field Diagram</title>
</head>
<body>
<canvas id="physicsCanvas" width="450" height="400" style="border:1px solid #d3d3d3;"></canvas>
<script>
    const canvas = document.getElementById('physicsCanvas');
    const ctx = canvas.getContext('2d');

    // --- Style & Font Settings ---
    ctx.strokeStyle = 'black';
    ctx.fillStyle = 'black';
    ctx.lineWidth = 2;
    const mainFont = 'italic 22px serif';
    const subscriptFont = 'italic 16px serif';
    const captionFont = '18px serif';

    // --- Coordinates & Dimensions ---
    const boundaryY = 180;
    const loopWidth = 100;
    const loopHeight = 120;
    const loopX = (canvas.width - loopWidth) / 2; // Center the loop horizontally
    const loopY = boundaryY - 80; // Position the loop so it's crossing the boundary

    // --- Drawing Functions ---

    // Function to draw a cross 'x' for the magnetic field
    function drawCross(x, y, size) {
        ctx.beginPath();
        ctx.moveTo(x - size, y - size);
        ctx.lineTo(x + size, y + size);
        ctx.moveTo(x + size, y - size);
        ctx.lineTo(x - size, y + size);
        ctx.stroke();
    }

    // --- Main Drawing Logic ---

    // 1. Draw the boundary line between the two regions
    ctx.beginPath();
    ctx.moveTo(0, boundaryY);
    ctx.lineTo(canvas.width, boundaryY);
    ctx.stroke();

    // 2. Draw the magnetic field region (B = B₀)
    const crossSize = 5;
    const spacing = 30;
    for (let y = boundaryY + spacing; y < canvas.height - 50; y += spacing) {
        for (let x = spacing; x < canvas.width; x += spacing) {
            // Leave a small clear area for the B=B₀ label, as in the original image
            if (x > 40 && x < 150 && y > 260 && y < 300) {
                continue;
            }
            drawCross(x, y, crossSize);
        }
    }

    // 3. Draw the rectangular wire loop
    ctx.strokeRect(loopX, loopY, loopWidth, loopHeight);

    // 4. Draw the text labels
    ctx.font = mainFont;

    // 'l' label for the loop's width
    ctx.textAlign = 'center';
    ctx.textBaseline = 'bottom';
    ctx.fillText('l', loopX + loopWidth / 2, loopY - 5);

    // 'w' label for the loop's height
    ctx.textAlign = 'left';
    ctx.textBaseline = 'middle';
    ctx.fillText('w', loopX + loopWidth + 10, loopY + loopHeight / 2);

    // 'B = 0' label for the upper region
    ctx.textAlign = 'left';
    ctx.textBaseline = 'bottom';
    ctx.fillText('B = 0', 50, boundaryY - 10);

    // 'B = B₀' label for the lower region
    const b0_x = 50;
    const b0_y = 280;
    ctx.fillText('B = B', b0_x, b0_y);
    const b_text_metrics = ctx.measureText('B = B');
    ctx.font = subscriptFont;
    ctx.fillText('0', b0_x + b_text_metrics.width - 6, b0_y + 3); // Position the '0' as a subscript

    // 5. Draw the gravity vector 'g'
    ctx.font = mainFont; // Reset font
    const g_arrow_x = 380;
    const g_arrow_y_start = 220;
    const g_arrow_y_end = 260;
    const g_arrow_head_size = 8;

    ctx.beginPath();
    // Arrow shaft
    ctx.moveTo(g_arrow_x, g_arrow_y_start);
    ctx.lineTo(g_arrow_x, g_arrow_y_end);
    // Arrowhead
    ctx.moveTo(g_arrow_x, g_arrow_y_end);
    ctx.lineTo(g_arrow_x - g_arrow_head_size / 2, g_arrow_y_end - g_arrow_head_size);
    ctx.moveTo(g_arrow_x, g_arrow_y_end);
    ctx.lineTo(g_arrow_x + g_arrow_head_size / 2, g_arrow_y_end - g_arrow_head_size);
    ctx.stroke();
    
    // 'g' label next to the arrow
    ctx.textAlign = 'left';
    ctx.textBaseline = 'middle';
    ctx.fillText('g', g_arrow_x + 15, (g_arrow_y_start + g_arrow_y_end) / 2);

    // 6. Draw the figure caption
    ctx.font = captionFont;
    ctx.textAlign = 'center';
    ctx.textBaseline = 'bottom';
    ctx.fillText('Fig. 2.37', canvas.width / 2, canvas.height - 10);

</script>
</body>
</html>